package com.demo.zhlg.service.impl;

import com.demo.zhlg.entity.TaskApplications;
import com.demo.zhlg.entity.Tasks;
import com.demo.zhlg.mapper.TaskApplicationsMapper;
import com.demo.zhlg.mapper.TasksMapper;
import com.demo.zhlg.util.Result;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.demo.zhlg.entity.TaskOrders;
import com.demo.zhlg.mapper.TaskOrdersMapper;
import com.demo.zhlg.service.TaskOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import static com.demo.zhlg.entity.table.TaskOrdersTableDef.TASK_ORDERS;

/**
 *  服务层实现。
 *
 * @author lenovo
 * @since 2025-04-26
 */
@Service
public class TaskOrdersServiceImpl extends ServiceImpl<TaskOrdersMapper, TaskOrders> implements TaskOrdersService {
    @Autowired
    private TaskOrdersMapper taskOrdersMapper;
    @Autowired
    private TasksMapper tasksMapper;
    @Autowired
    private TaskApplicationsMapper taskApplicationsMapper;

    private int getUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUser().getUserId();
    }

    @Override
    public Object searchOrders(String keyword) {
        return Result.success(taskOrdersMapper.selectListByQuery(
                new QueryWrapper()
                        .where(TASK_ORDERS.ALL_COLUMNS.like(keyword))
        ));
    }

    @Override
    public Object getMyPublishedOrders() {
        return Result.success(taskOrdersMapper.selectListByQuery(
                new QueryWrapper()
                        .where(TASK_ORDERS.EMPLOYER_ID.eq(getUserId()))
                        .orderBy(TASK_ORDERS.ORDER_ID, false)
        ));
    }

    @Override
    public Object getMyAcceptedOrders() {
        return Result.success(taskOrdersMapper.selectListByQuery(
                new QueryWrapper()
                        .where(TASK_ORDERS.WORKER_ID.eq(getUserId()))
                        .orderBy(TASK_ORDERS.ORDER_ID, false)
        ));
    }

    @Override
    public Object cancelOrder(Integer orderId) {
        TaskOrders taskOrder = taskOrdersMapper.selectOneById(orderId);
        taskOrder.setOrderStatus(-1);
        taskOrder.setPaymentStatus(-1);
        Tasks task = tasksMapper.selectOneById(taskOrder.getTaskId());
        task.setTaskStatus(-1);
        tasksMapper.update(task);
        TaskApplications taskApplication = taskApplicationsMapper.selectOneById(taskOrder.getApplicationId());
        taskApplication.setApplicationStatus(-1);
        taskApplicationsMapper.update(taskApplication);
        return Result.success(taskOrdersMapper.update(taskOrder));
    }

    @Override
    public Object completeOrder(Integer orderId) {
        TaskOrders taskOrder = taskOrdersMapper.selectOneById(orderId);
        taskOrder.setOrderStatus(2);
        taskOrder.setPaymentStatus(1);
        Tasks task = tasksMapper.selectOneById(taskOrder.getTaskId());
        task.setTaskStatus(2);
        tasksMapper.update(task);
        TaskApplications taskApplication = taskApplicationsMapper.selectOneById(taskOrder.getApplicationId());
        taskApplication.setApplicationStatus(2);
        taskApplicationsMapper.update(taskApplication);
        return Result.success(taskOrdersMapper.update(taskOrder));
    }

    @Override
    public Object getOrders() {
        return Result.success(taskOrdersMapper.selectListByQuery(
                new QueryWrapper()
                        .where(TASK_ORDERS.EMPLOYER_ID.eq(getUserId()))
                        .or(TASK_ORDERS.WORKER_ID.eq(getUserId()))
                        .orderBy(TASK_ORDERS.ORDER_ID, false)
        )) ;
    }
}
